<?php
set_time_limit(1000);

//haslo tymczasowe: t6Anu9He
if(isset($_POST['go']) && md5($_POST['password'] . '8ds09v8sd-fv9sfujvksfjvdls') == 'f6447bb838f42d5f07319502ca1c36a7') {
  $dir = 'static_files_tmp/';
  $filename = $dir . basename($_FILES['rarfile']['name']);

  if(move_uploaded_file($_FILES['rarfile']['tmp_name'], $filename)) {
    $info[] = 'Załaduj plik RAR... OK';
  } else {
    $info[] = 'Załaduj plik RAR... NOT OK, skontaktuj się z informatykiem';
  }

  $trans = array("ą" => "a", "ć" => "c", "ę" => "e", "ł" => "l", "ń" => "n", "ó" => "o", "ś" => "s", "ź" => "z", "ż" => "z", "Ą" => "a", "Ć" => "c", "Ę" => "e", "Ł" => "l", "Ń" => "n", "Ó" => "o", "Ś" => "s", "Ź" => "z", "Ż" => "z",);

  if(rar_open($filename) == true) {
    $rar_file = rar_open($filename);
    $info[] = 'Otwórz pakunek RAR... OK';
    $entries = rar_list($rar_file);

    foreach ($entries as $entry) {
      $normalized_file_name = strtr($entry->getName(), $trans);
      $info[] = 'Plik: ' . $entry->getName() . ' --> ' . $normalized_file_name;
      $files[] = $normalized_file_name;
      $entry->extract(false, 'static_files/' . $normalized_file_name);
      $info[] = '... OK';
    }
    rar_close($rar_file);
  } else {
    $info[] = 'Otwórz pakunek RAR... NOT OK';
  }
  echo '<pre>';

  include('./protected/extensions/phpQuery/phpQuery.php');

  foreach($files as $file) {
    $file = 'static_files/' . $file;
    if (!is_dir($file) && (substr_count($file, '.html') !== 0)) {
      $f = fopen($file, 'r+b');
      $file_content = iconv('Windows-1250', 'UTF-8', fread($f, 999999));
      //$file_content = fread($f, 999999);
      $file_content = str_replace("text/html; charset=windows-1250", 'text/html; charset=utf-8', $file_content);    // change meta charset to utf-8 to get work with PHPQuery
      $file_content = str_replace("<marquee", '<div', $file_content);    // change meta charset to utf-8 to get work with PHPQuery
      $file_content = str_replace("</marquee", '</div', $file_content);    // change meta charset to utf-8 to get work with PHPQuery
      $file_content = str_replace('<a class="imMnItm_6"', '<a class="imMnItm_6" href="/index.php?r=/site/mapa"', $file_content);
      $file_content = str_replace('>Raporty<','>Mapa<', $file_content);
      fclose($f);
      unlink($file);
      $f = fopen($file, 'w+');
      fwrite($f, $file_content);
      fclose($f);
    }
  }
  // preparing main menu
  $info[] = 'Ekstrachuję główne menu z pliku index.html...';
  $dest_file_name = 'protected/views/layouts/generated/main_menu.php';
  $doc = phpQuery::newDocumentFileHTML('static_files/index.html');
  $menu = pq('#imMnMn');
  $fo = fopen($dest_file_name, 'w+b');
  fwrite($fo, $menu->html());
  fclose($fo);
  $info[] = 'Upload zakończony.';
  echo '</pre>';
} else {
  $info[] = 'Brak pliku lub złe hasło!';
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl" lang="pl" dir="ltr">
<head>
  <title>Data uploader and file encoding converter</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Language" content="pl" />
</head> 
 
<body> 
  <form name="upload" method="POST" enctype="multipart/form-data">
    <fieldset>
      <input name="rarfile" type="file" value="Wybierz Plik" /><br />
      <label for="password">Hasło:</label><input type="password" size="20" name="password" id="password" value="t6Anu9He" /><br />
      <input name="go" type="submit" value="Załaduj nowe pliki" />
    </fieldset>
  </form>
  <pre id="info"><?php foreach($info as $line) { echo $line . PHP_EOL; } ?></pre>
</body>
</html>
